package com.shiwaixiangcun.customer.module.housebutler

import android.os.Bundle
import android.support.v7.widget.LinearLayoutManager
import android.view.Gravity
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.LinearLayout
import com.google.gson.reflect.TypeToken
import com.lzy.okgo.OkGo
import com.lzy.okgo.callback.StringCallback
import com.lzy.okgo.model.Response
import com.shiwaixiangcun.customer.ConfigSps
import com.shiwaixiangcun.customer.GlobalApi
import com.shiwaixiangcun.customer.R
import com.shiwaixiangcun.customer.base.BaseActivity
import com.shiwaixiangcun.customer.event.EventUtil
import com.shiwaixiangcun.customer.event.SimpleEvent
import com.shiwaixiangcun.customer.glide.ImageDisplayUtil
import com.shiwaixiangcun.customer.module.heath.CommonEntity
import com.shiwaixiangcun.customer.network.ResponseConfig
import com.shiwaixiangcun.customer.utils.AppSharePreferenceMgr
import com.shiwaixiangcun.customer.utils.DateUtil
import com.shiwaixiangcun.customer.utils.DisplayUtil
import com.shiwaixiangcun.customer.utils.JsonUtil
import kotlinx.android.synthetic.main.activity_service_record_detail.*
import kotlinx.android.synthetic.main.top_bar_white.*
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode


/**
 * 服务记录详情
 *
 */
class ServiceRecordDetailActivity : BaseActivity() {

    companion object {
        private val REQUEST_DATA = 0x1001
    }


    private var mProcessList: MutableList<Proces>? = null

    private var mAdapterRecord: AdapterRecordProcess? = null
    private var id: Long? = null
    private var strToken: String? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_service_record_detail)
        EventUtil.getInstance().register(this)
        initBundleData()
        initViewAndEvent()
    }


    override fun onDestroy() {
        EventUtil.getInstance().unregister(this)
        super.onDestroy()
    }

    private fun initBundleData() {
        id = intent.extras.getLong("id")
    }

    private fun initViewAndEvent() {

        tv_page_name.text = "记录详情"
        back_left.setOnClickListener { finish() }

        mProcessList = ArrayList()
        mAdapterRecord = AdapterRecordProcess(mContext, mProcessList)
        rv_stature.layoutManager = LinearLayoutManager(this)
        rv_stature.adapter = mAdapterRecord
        initData()
    }


    private fun initData() {

        strToken = AppSharePreferenceMgr.get(mContext, ConfigSps.CURRENT_TOKEN, "") as String
        id?.let {
            OkGo.get<String>(GlobalApi.serviceListDetail)
                    .params("access_token", strToken)
                    .params("id", it)
                    .execute(object : StringCallback() {
                        override fun onSuccess(response: Response<String>?) {
                            val type = object : TypeToken<CommonEntity<RecordDetailEntity>>() {}.type
                            val responseData = JsonUtil.fromJson<CommonEntity<RecordDetailEntity>>(response?.body(), type)
                            when (responseData.responseCode) {
                                ResponseConfig.SUCCESS -> {
                                    EventUtil.getInstance().post(SimpleEvent(SimpleEvent.RECORD_DETAIL, REQUEST_DATA, responseData.data))

                                }
                            }
                        }

                    })
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    fun updateUI(simpleEvent: SimpleEvent) {
        if (simpleEvent.mEventType != SimpleEvent.RECORD_DETAIL) {
            return
        }
        when (simpleEvent.mEventValue) {
            REQUEST_DATA -> {

                val recordDetialEntity = simpleEvent.data as RecordDetailEntity

                val data = recordDetialEntity.baseInfo
                when (data.status) {
                    "ACCEPTED" -> {
                        tv_stature.setBackgroundResource(R.drawable.shape_stature_green)
                        tv_stature.text = "受理中"
                    }
                    "FINISHED" -> {
                        tv_stature.setBackgroundResource(R.drawable.shape_stature_gray)
                        tv_stature.text = "已完成"
                    }
                    "UNTREATED" -> {
                        tv_stature.setBackgroundResource(R.drawable.shape_stature_gray)
                        tv_stature.text = "未受理"
                    }

                    else -> {
                    }
                }

                tv_number.text = data.number
                tv_reason.text = data.content
                tv_date.text = DateUtil.getTimeFormatString(data.time)

                if (data.images.isEmpty()) {
                    hScrollView_iamge.visibility = View.GONE
                } else {
                    hScrollView_iamge.visibility = View.VISIBLE
                    val linearLayout = LinearLayout(mContext)
                    data.images.forEach {
                        val imageView = ImageView(this)
                        imageView.scaleType = ImageView.ScaleType.CENTER_CROP
                        val layoutParams = LinearLayout.LayoutParams(DisplayUtil.dip2px(mContext, 80f), DisplayUtil.dip2px(mContext, 80f))
                        layoutParams.setMargins(0, 0, DisplayUtil.dip2px(mContext, 10f), 0)
                        imageView.layoutParams = layoutParams

                        ImageDisplayUtil.showImageView(mContext, it.thumbImageURL, imageView)
                        linearLayout.addView(imageView)
                    }
                    val params = LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT)
                    params.gravity = Gravity.CENTER_VERTICAL
                    linearLayout.layoutParams = params
                    hScrollView_iamge.addView(linearLayout)
                }

                mProcessList?.addAll(recordDetialEntity.process)
                mAdapterRecord?.notifyDataSetChanged()
            }

        }
    }
}
